﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;

using Ivy.Common.Framework.Base;
using Ivy.Common.Framework.DataAccess;
using Ivy.SiteAdmin.Entity;

namespace Ivy.SiteAdmin.DataAccess
{
   public class ContentTypeMasterDao : IDaoInterface<ContentTypeMaster, ContentTypeMasterCriteria>
   {
       #region  Variable & constant
       #endregion  Variable & constant

       #region Constructor & Destructor
       #endregion Constructor & Destructor

       #region Properties
       #endregion Properties

       #region Public Methods
       #region IDaoInterface<ContentTypeMaster, ContentTypeMasterCriteria> Members
       public bool Insert(ContentTypeMaster ContentTypeMasterEntity)
       {
           bool IsOperationSuccessful = false;
           
               if (ContentTypeMasterEntity.EntityState == EntityState.New)
               {
                   DbConnection DbConn = DataAccessProviderUtility.CreateConnectionWithConnectionString();
                   DbCommand CmdObj = DataAccessProviderUtility.CreateCommand();
                   CmdObj.CommandText = "Dasideen_ContentTypeMaster_Insert";
                   CmdObj.CommandType = CommandType.StoredProcedure;

                   DataAccessProviderUtility.AddParameter(CmdObj, "@ContentTypeCode", ParameterDirection.Input, DbType.String, ContentTypeMasterEntity.ContentTypeCode);
                   DataAccessProviderUtility.AddParameter(CmdObj, "@ContentTypeName", ParameterDirection.Input, DbType.String, ContentTypeMasterEntity.ContentTypeName);
                   DataAccessProviderUtility.AddParameter(CmdObj, "@ContentDescription", ParameterDirection.Input, DbType.String, ContentTypeMasterEntity.ContentDescription);


                   DatabaseCommon.InsertStoredProcForParameterDatEntityBase(CmdObj, ContentTypeMasterEntity);

                   DataAccessProviderUtility.AddParameter(CmdObj, "@ContentTypeKey", ParameterDirection.Output, DbType.Int32, DBNull.Value);
                   DataAccessProviderUtility.AddParameter(CmdObj, "@Sys_DateOfCreation", ParameterDirection.Output, DbType.DateTime, DBNull.Value);
                   CmdObj.Connection = DbConn;
                   DbConn.Open();
                   CmdObj.Prepare();
                   int _NoOfRecord = CmdObj.ExecuteNonQuery();
                   if (_NoOfRecord == 1)
                   {
                       ContentTypeMasterEntity.ContentTypeKey = Convert.ToInt32(CmdObj.Parameters["@ContentTypeKey"].Value);
                       ContentTypeMasterEntity.Sys_DateOfCreation = Convert.ToDateTime(CmdObj.Parameters["@Sys_DateOfCreation"].Value);
                       ContentTypeMasterEntity.Sys_DateOfModification = ContentTypeMasterEntity.Sys_DateOfCreation;
                       ContentTypeMasterEntity.EntityState = EntityState.UnChanged;
                       IsOperationSuccessful = true;
                   }
                   CmdObj.Dispose();
                   DbConn.Close();
               }
               else
               {

                   throw new Exception("For Inserting new ContentTypeMaster entity data to database,[EntityState] attribute should be [New]");
               }
           
           return IsOperationSuccessful;
       }

       public bool Update(ContentTypeMaster DataEntity)
       {
           throw new NotImplementedException();
       }

       public bool Delete(ContentTypeMaster DataEntity)
       {
           throw new NotImplementedException();
       }

       public ContentTypeMaster GetByID(string ID)
       {
           throw new NotImplementedException();
       }

       public List<ContentTypeMaster> GetAll()
       {
           throw new NotImplementedException();
       }

       public List<ContentTypeMaster> GetByCriteria(ContentTypeMasterCriteria CriteriaEntity)
       {
           throw new NotImplementedException();
       }
       #endregion Members
       public ContentTypeMaster GetByContentTypeKey(int ContentTypeKey)
       {
           ContentTypeMaster _ContentTypeMasterList = new ContentTypeMaster(); //IMPORTANT NOTE:- Just declare the variable never create an instance, create instance only when required.
           
               DbConnection DbConn = DataAccessProviderUtility.CreateConnectionWithConnectionString();
               DbCommand CmdObj = DataAccessProviderUtility.CreateCommand();
               CmdObj.CommandText = "Dasideen_ContentTypeMaster_GetByContentTypeKey";
               CmdObj.CommandType = CommandType.StoredProcedure;

               DataAccessProviderUtility.AddParameter(CmdObj, "@ContentTypeKey", ParameterDirection.Input, DbType.Int32, Convert.ToInt32(ContentTypeKey));


               CmdObj.Connection = DbConn;
               DbConn.Open();
               CmdObj.Prepare();
               DbDataReader _DataReader = CmdObj.ExecuteReader();

               if (_DataReader.HasRows)
               {
                   if (_DataReader.Read())
                   {
                       _ContentTypeMasterList = new ContentTypeMaster(); //IMPORTANT NOTE:- Create instance only when required.
                       TranslateContentTypeMasterFieldsFromReaderToEntity(_ContentTypeMasterList, _DataReader);
                   }
               }
               else
               {
                   throw new Exception("No _ContentTypeMaster found");
               }
               CmdObj.Dispose();
               DbConn.Close();
           
           return _ContentTypeMasterList;
       }

       public ContentTypeMaster GetByContentTypeCode(string ContentTypeCode)
       {
           ContentTypeMaster _ContentTypeMasterList = new ContentTypeMaster(); //IMPORTANT NOTE:- Just declare the variable never create an instance, create instance only when required.
           
               DbConnection DbConn = DataAccessProviderUtility.CreateConnectionWithConnectionString();
               DbCommand CmdObj = DataAccessProviderUtility.CreateCommand();
               CmdObj.CommandText = "Dasideen_ContentTypeMaster_GetByContentTypeCode";
               CmdObj.CommandType = CommandType.StoredProcedure;

               DataAccessProviderUtility.AddParameter(CmdObj, "@ContentTypeCode", ParameterDirection.Input, DbType.String, Convert.ToString(ContentTypeCode));


               CmdObj.Connection = DbConn;
               DbConn.Open();
               CmdObj.Prepare();
               DbDataReader _DataReader = CmdObj.ExecuteReader();

               if (_DataReader.HasRows)
               {
                   if (_DataReader.Read())
                   {
                       _ContentTypeMasterList = new ContentTypeMaster(); //IMPORTANT NOTE:- Create instance only when required.
                       TranslateContentTypeMasterFieldsFromReaderToEntity(_ContentTypeMasterList, _DataReader);
                   }
               }
               else
               {
                   throw new Exception("No ContentTypeMaster found");
               }
               CmdObj.Dispose();
               DbConn.Close();
           
           return _ContentTypeMasterList;
       }

       public List<ContentTypeMaster> GetByContentTypeName(string ContentTypeName)
       {
           List<ContentTypeMaster> _ContentTypeMasterList = new List<ContentTypeMaster>();
           
               DbConnection DbConn = DataAccessProviderUtility.CreateConnectionWithConnectionString();
               DbCommand CmdObj = DataAccessProviderUtility.CreateCommand();
               CmdObj.CommandText = "Dasideen_ContentTypeMaster_GetByContentTypeName";
               CmdObj.CommandType = CommandType.StoredProcedure;

               CmdObj.Connection = DbConn;
               DbConn.Open();
               CmdObj.Prepare();

               DbDataReader _DataReader = CmdObj.ExecuteReader();
               if (_DataReader.HasRows)
               {
                   while (_DataReader.Read())
                   {
                       ContentTypeMaster _TempContentTypeMaster = new ContentTypeMaster(); //IMPORTANT NOTE:- Create instance only when required.
                       this.TranslateContentTypeMasterFieldsFromReaderToEntity(_TempContentTypeMaster, _DataReader);
                       _ContentTypeMasterList.Add(_TempContentTypeMaster);
                   }
               }
               CmdObj.Dispose();
               DbConn.Close();
           
           return _ContentTypeMasterList;
       }
        

       #endregion Public Methods

       #region Events
       #endregion Events

       #region Private Methods
       private void TranslateContentTypeMasterFieldsFromReaderToEntity(ContentTypeMaster pContentTypeMaster, DbDataReader pDbDataReader)
       {
          
               pContentTypeMaster.ContentTypeKey = Convert.ToInt32(pDbDataReader["ContentTypeKey"]);
               pContentTypeMaster.ContentTypeCode = Convert.ToString(pDbDataReader["ContentTypeCode"]);
               pContentTypeMaster.ContentTypeName = Convert.ToString(pDbDataReader["ContentTypeName"]);
               pContentTypeMaster.ContentDescription = Convert.ToString(pDbDataReader["ContentDescription"]);


               DatabaseCommon.TranslateSystemFieldsFromReaderToEntity(pContentTypeMaster, pDbDataReader);
           
       }
       #endregion Private Methods

   }
}
